<template>
    <el-card>
        <el-table :data="PermissionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
            <el-table-column label="名称" prop="name" />
            <el-table-column label="权限值" prop="code" />
            <el-table-column label="修改时间" prop="updateTime" />
            <el-table-column label="操作">
                <template #="{ row }">
                    <el-button type="primary" size="small" icon="Plus" :disabled="row.level == 4"
                        @click="addPermission(row)">{{ row.level == 3 ? '添加功能' : '添加菜单' }}</el-button>
                    <el-button type="success" size="small" icon="Edit" :disabled="row.level == 1"
                        @click="updatePermission(row)">编辑</el-button>
                    <el-popconfirm :title="`你确定要删除${row.roleName}?`" width="260px" @confirm="removePermission(row.id)">
                        <template #reference>
                            <el-button type="danger" size="small" icon="Delete"
                                :disabled="row.level == 1">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 添加与编辑菜单 -->
        <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '编辑菜单'">
            <el-form>
                <el-form-item label="名称：">
                    <el-input placeholder="请输入名称" v-model="menuData.name"></el-input>
                </el-form-item>
                <el-form-item label="权限值：">
                    <el-input placeholder="请输入权限值" v-model="menuData.code"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确认</el-button>
            </template>
        </el-dialog>
    </el-card>
</template>
<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { reqAllPermission, reqAddOrUpdateMenu, reqRemoveMenu } from '@/api/acl/menu'
//引入ts类型
import type { MenuParams, PermissionResponseData, PermissionList, Permission } from '@/api/acl/menu/type';
//存储菜单的数据
let PermissionArr = ref<PermissionList>([]);
// 弘治对话框的显示
let dialogVisible = ref<boolean>(false)
// 携带的参数
let menuData = reactive<MenuParams>({
    code: '',//权限数值
    level: 0, //几级菜单
    name: '', //菜单的名字
    pid: 0 //菜单的ID
})
onMounted(() => {
    getHasPermission()
})
const getHasPermission = async () => {
    let result: PermissionResponseData = await reqAllPermission()
    if (result.code == 200) {
        PermissionArr.value = result.data;
    }
}
// 添加菜单
const addPermission = (row: Permission) => {
    //清空数据
    Object.assign(menuData, {
        id: 0,
        code: "",
        level: 0,
        name: "",
        pid: 0,
    })
    //对话框显示出来
    dialogVisible.value = true;
    //收集新增的菜单的level数值
    menuData.level = row.level + 1;
    //给谁新增子菜单
    menuData.pid = (row.id as number);
}
// 更新菜单
const updatePermission = (row: Permission) => {
    dialogVisible.value = true
    Object.assign(menuData, row)
}
// 确定按钮
const save = async () => {
    //发请求:新增子菜单|更新某一个已有的菜单的数据
    let result: any = await reqAddOrUpdateMenu(menuData);
    if (result.code == 200) {
        //对话框隐藏
        dialogVisible.value = false;
        //提示信息
        ElMessage({ type: 'success', message: menuData.id ? '更新成功' : '添加成功' })
        //再次获取全部最新的菜单的数据
        getHasPermission();
    }
}
// 删除菜单
const removePermission = async (id: number) => {
    let result = await reqRemoveMenu(id)
    if (result.code == 200) {
        ElMessage.success('删除成功')
        getHasPermission()
    }
}
</script>
<style lang="scss" scoped></style>